var isVoteSave = false;

function updateUser() {

	if (isVoteSave) {
		alert('正在保存结果中，请稍侯...');
		return;
	}

	var voteUser = {};
	var vmode = $('#vmode').val();
	voteUser.voteId = $('#voteId').val();
	voteUser.id = $('#voteUserId').val();
	voteUser.voteUserOptionDtos = [];
	voteUser.voteUserMsgDtos = [];
	//处理选项
	for (var i = 1;; i++) {
		var $option = $('#option' + i);
		if ($option.length == 0) {
			break;
		}
		//单选被选中 或者 多选被选中
		if ((vmode == 0 && $option.find('input:radio').prop('checked'))
				|| (vmode == 1 && $option.find('input:checkbox')
						.prop('checked'))) {
			voteUser.voteUserOptionDtos[i - 1] = {};
			voteUser.voteUserOptionDtos[i - 1].voteId = voteUser.voteId;
			voteUser.voteUserOptionDtos[i - 1].isSelect = 1;
			voteUser.voteUserOptionDtos[i - 1].voteOptionId = $option.find(
					'input:hidden').val();
			if (!($option.find('input:text').val() == "")) {
				voteUser.voteUserOptionDtos[i - 1].optionNum = $option.find(
						'input:text').val();
			}
		}
	}

	if (voteUser.voteUserOptionDtos.length == 0) {
		alert('请选择一个选项');
		return;
	}

	//处理输入信息
	for (var i = 1;; i++) {
		var $msg = $('#msg' + i);
		if ($msg.length == 0) {
			break;
		}
		var memo = $msg.find('label').html();
		if (!$msg.find('input:text').val()) {
			alert(memo + '不能为空');
			return;
		}
		voteUser.voteUserMsgDtos[i - 1] = {};
		voteUser.voteUserMsgDtos[i - 1].voteId = voteUser.voteId;
		voteUser.voteUserMsgDtos[i - 1].voteMsgId = $msg.find('input:hidden')
				.val();
		voteUser.voteUserMsgDtos[i - 1].msgValue = $msg.find('input:text')
				.val();
	}

	var jdata = JSON.stringify(voteUser);
	isVoteSave = true;
	postContent("/vote/updateUser", jdata, true, function(data) {
		if (data != null) {
			alert('修改成功!');
		}
		isVoteSave = false;
	});
}


function initUpdateVote() {

	isVoteSave = false;
	
	var voteId = $('#voteId').val();
	var voteUserId = $('#voteUserId').val();
	if (voteUserId == null) {
		alert('无法获取用户投票信息!');
	}
	getContent("/vote/get", 'voteId=' + voteId, true, function(data) {
		if (data != null) {
			$('#voteId').val(data.id);
			$('#vmode').val(data.vmode);
			$('#title').html(data.title);
			$('#theme').html(data.theme);
			$('#overDate').html(data.overDateStr);
			$cOption = null;
			$show = null; //全部展示的div
			$append = null;
			if (data.vmode == 0) {
				$cOption = $('#cRadio').clone(false);
				$show = $('#radioOpt').clone(false);
				$append = $show.find('#options');
			} else {
				$cOption = $('#cCheckbox').clone(false);
				$show = $('#checkboxOpt').clone(false);
				$append = $show;
			}
			$show.attr("style", "display:block;");
			for (var i = 0; i < data.voteOptionDtos.length; i++) {
				var optionData = data.voteOptionDtos[i];
				$curOption = $cOption.clone(false);
				$curOption.attr("id", "option" + (i + 1));
				$curOption.attr("style", "display:block;");
				$curOption.find('span').html(optionData.optionstr);
				$curOption.find('input:hidden').val(optionData.id);
				$curOption.find('input:text').val('');
				$append.append($curOption);
				if (i > 0) {//这是可选框用的
					$curOption.find('label').html('');
				}
				if (optionData.isinput == 0) {
					$curOption.find('input:text').hide();
				}
			}
			$('#userOptions').append($show);

			for (var i = 0; i < data.voteMsgDtos.length; i++) {
				var msgData = data.voteMsgDtos[i];
				$curMsg = $('#cMsg').clone(false);
				$curMsg.find('input:hidden').val(msgData.id);
				$curMsg.find('input:text').val('');
				$curMsg.attr("id", "msg" + (i + 1));
				$curMsg.attr("style", "display:block;");
				$curMsg.children('label').html(msgData.msg);
				$('#user').append($curMsg);
			}

			var getUserParam = {};
			getUserParam.voteId = voteId;
			getUserParam.voteUserId = voteUserId;
			getContent("/vote/getUser", getUserParam, true, function(data) {
				var msgs = data.voteUserMsgDtos;
				if (msgs.length > 0) {
					for (num in msgs) {
						var msgValue = msgs[num].msgValue;
						var msgId = msgs[num].voteMsgId;
						$('input:hidden[value=' + msgId + ']').parent(
								'[id^=msg]').find('input:text').val(msgValue)
					}
				}
				var options = data.voteUserOptionDtos;
				if (options.length > 0) {
					for (num in options) {
						var optionId = options[num].voteOptionId;
						var voteUserId = options[num].voteUserId;
						var optionNum = options[num].optionNum;
						var isSelect = options[num].isSelect;
						if (isSelect == 1) {
							var $option = $(
									'input:hidden[value=' + optionId + ']')
									.parent('[id^=option]');
							$option.find('input:text').val(optionNum);
							$option.find('#option').prop('checked', true)
						}
					}
				}
			});
		}
	});

}